package com.sxdx.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.annotation.Resource;

@Configuration
public class MvcConfig implements WebMvcConfigurer {
    @Resource
    private StringRedisTemplate stringRedisTemplate;
    @Resource
    LoginInterceptor loginInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(loginInterceptor)
                .addPathPatterns("/**")
                //不需要拦截路径
                .excludePathPatterns(
                "/login",
                "/captcha/captchaImageBase64"
        );
    }

    /**
     * 配置跨域
     * @param registry
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        //  设置允许跨域的路径
        registry.addMapping("/**")
                //  设置允许跨域请求的域名
                .allowedOriginPatterns("*")
                //  是否允许证书
                .allowCredentials(true)
                //  设置允许的方法
                .allowedMethods("GET", "POST", "DELETE", "PUT", "PATCH", "OPTION")
                .maxAge(3600);
    }

}
